package com.ruoyi.mail.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JiajiaMallShoppingCart;
import com.ruoyi.system.service.IJiajiaMallShoppingCartService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Controller
@RequestMapping("/mall/cart")
public class JiajiaMallShoppingCartController extends BaseController
{
    private String prefix = "mall/cart";

    @Autowired
    private IJiajiaMallShoppingCartService jiajiaMallShoppingCartService;

    @RequiresPermissions("system:cart:view")
    @GetMapping()
    public String cart()
    {
        return prefix + "/cart";
    }

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("system:cart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaMallShoppingCart jiajiaMallShoppingCart)
    {
        startPage();
        List<JiajiaMallShoppingCart> list = jiajiaMallShoppingCartService.selectJiajiaMallShoppingCartList(jiajiaMallShoppingCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("system:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaMallShoppingCart jiajiaMallShoppingCart)
    {
        List<JiajiaMallShoppingCart> list = jiajiaMallShoppingCartService.selectJiajiaMallShoppingCartList(jiajiaMallShoppingCart);
        ExcelUtil<JiajiaMallShoppingCart> util = new ExcelUtil<JiajiaMallShoppingCart>(JiajiaMallShoppingCart.class);
        return util.exportExcel(list, "购物车数据");
    }

    /**
     * 新增购物车
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存购物车
     */
    @RequiresPermissions("system:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaMallShoppingCart jiajiaMallShoppingCart)
    {
        return toAjax(jiajiaMallShoppingCartService.insertJiajiaMallShoppingCart(jiajiaMallShoppingCart));
    }

    /**
     * 修改购物车
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaMallShoppingCart jiajiaMallShoppingCart = jiajiaMallShoppingCartService.selectJiajiaMallShoppingCartById(id);
        mmap.put("jiajiaMallShoppingCart", jiajiaMallShoppingCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存购物车
     */
    @RequiresPermissions("system:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaMallShoppingCart jiajiaMallShoppingCart)
    {
        return toAjax(jiajiaMallShoppingCartService.updateJiajiaMallShoppingCart(jiajiaMallShoppingCart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("system:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaMallShoppingCartService.deleteJiajiaMallShoppingCartByIds(ids));
    }
}
